﻿#ifndef TCPCLIENT_H
#define TCPCLIENT_H

#include <QHostAddress>
#include <QTcpSocket>
#include <qobject.h>

class TcpClient : public QObject {
  Q_OBJECT
public:
  explicit TcpClient(QObject *parent = nullptr);
  ~TcpClient();
  void connectToServer(QString strIP = "127.0.0.1", int nport = 6000);
  void sendData(const QString &data);
  void sendData(const QByteArray &data);
  void sendData(const char *data, int nlen);
  QAbstractSocket::SocketState state();
  bool isValid();

private:
  void connectAllUsedSlots();
  void transMessage(const QString &msg);
public slots:
  void dataReceived();
  void Disconnect();
signals:
  void errorOccurred(QAbstractSocket::SocketError socketError);
  void dataAvailable(const QByteArray &data);
  void TcpMsg(const QString &strmsg);

private:
  QTcpSocket socket;
};

#endif // TCPCLIENT_H
